<?php
namespace app\admin\controller;
use think\Request;
use app\common\model\Role as RoleModel;
use app\common\model\Permission as PermissionModel;
use think\Loader;

class Role extends Base{
    public function index(){

        $page = RoleModel::where([])->paginate(10);

        $this->data['model'] = $page;
        $this->data['column'] = $this->column();
        $this->data['title'] = "角色管理";
        return view('',$this->data);
    }


    public function add(){
        $this->data['title'] = '角色添加';
        return view('',$this->data);
    }


    public function edit(Request $request){

        $this->data['title'] = '角色更新';
        $this->data['model'] = RoleModel::get($request->param('id'));

        return view('',$this->data);
    }


    public function handle(Request $request){

        $data = $request->post();

        $validate = Loader::validate('Role');
        if(!$validate->check($data)){
            $this->error($validate->getError());
        }

        if(empty($data['id'])){
            //角色能不能重名的问题？
            if(RoleModel::create($data)){
                $this->success("新增成功");
            }
        }else{
            $navigationModel = new RoleModel();
            if($navigationModel -> save($data,['id'=>$data['id']])){
                $this->success("更新成功");
            }
        }

        $this->error("操作失败");
    }


    public function delete(Request $request){
        $model = RoleModel::get($request->param('id'));
        if($model){
            $res = $model ->delete();
            if($res){
                $this->success("删除成功");
            }
        }

        $this->error("操作失败");
    }


    /**
     * 角色的权限分配
     * @param Request $request
     * @return mixed
     * @throws
     */
    public function permission(Request $request){
        $model = RoleModel::get($request->param('id'));
        $model->permission = explode(",",$model->permission);
        $model->permission = array_unique(array_filter($model->permission));

        $this->data['title'] = "权限分配";
        $this->data['model'] = $model;
        $this->getPermission();
        return view('',$this->data);
    }

    public function handlePermission(Request $request){
        $param = $request->param();

        $permission = $param['permission'];
        $permission = array_unique(array_filter($permission));
        if(!empty($permission)){
            $permission = implode(",",$permission);
        }
        $param['permission'] = $permission;
        if(RoleModel::update($param,['id'=>$param['id']])){
            $this->success("操作成功");
        }
    }


    protected function getPermission(){
        $permission = PermissionModel::where([])->column("*","id");
        $this->data['permissions'] = $permission;

        $group = [];
        foreach( $permission as $key => $val ){
            $group[$val['group']][] = $val;
        }
        $this->data['group'] = $group;

    }


    protected function column(){
        return [
            'title'=>'角色名称',
        ];
    }
}